//
//  NSString+Extension.m
//  EFuCai-iOS
//
//  Created by NewLife on 2017/12/15.
//  Copyright © 2017年 NewLife. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

- (CGSize)sizeWithFont:(UIFont *)font andMaxSize:(CGSize)maxSize
{
    NSDictionary *arrts = @{NSFontAttributeName:font};
    
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:arrts context:nil].size;
}

- (NSAttributedString *)attributedStringWithFirstTextColor:(UIColor *)firstTextColor firstRange:(NSRange)firstRange secondTextColor:(UIColor *)secondTextColor secondRange:(NSRange)secondRange {
    
    NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:self];
    
    [attrM setAttributes:@{NSForegroundColorAttributeName: firstTextColor} range:firstRange];
    
    [attrM setAttributes:@{NSForegroundColorAttributeName: secondTextColor} range:secondRange];
    
    return attrM.copy;
}

- (NSString *)returnRealPrice {
    
    return [self returnRealPriceWithFloatLength:2];
}

- (NSString *)returnRealPriceWithFloatLength:(NSInteger)floatlength {
    
    NSRange point1Range = [self rangeOfString:@"."];
    NSRange point2Range = [self rangeOfString:@"." options:NSBackwardsSearch];
    
    if (!NSEqualRanges(point1Range, point2Range)) {//不相等, 两个.
        
        return [self substringToIndex:self.length - 1];
    }
    
    if (self.length > point1Range.location + point1Range.length + floatlength) {
        
        return [self substringToIndex:point1Range.location + point1Range.length + floatlength];
    }
    return self;
}

- (BOOL)returnRealWithFloatLength:(NSInteger)floatlength{
    
    NSRange point1Range = [self rangeOfString:@"."];
    NSRange point2Range = [self rangeOfString:@"." options:NSBackwardsSearch];
    
    if (!NSEqualRanges(point1Range, point2Range)) {//不相等, 两个.
        
//        return [text substringToIndex:self.length - 1];
        return NO;
    }
    
    if (self.length > point1Range.location + point1Range.length + floatlength) {
        
//        return [text substringToIndex:point1Range.location + point1Range.length + floatlength];
        return NO;
    }
    return YES;
}


- (BOOL)StringIsNullOrEmpty:(NSString *)str
{
    return (str == nil || [str isKindOfClass:[NSNull class]] || str.length == 0);
}
@end
